package com.chaucer.controller;

import com.chaucer.pojo.Article;
import com.chaucer.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 访问跟路径时，跳转查询文章方法。
     * @return
     */
    @RequestMapping("/")
    public String index(){
        return "redirect:/index";
    }

    /**
     * 查询文章并指定模板渲染页面
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model){
        Page<Article> articlePage = articleService.queryArticlePage(0, 3);
        model.addAttribute("articlePage",articlePage);
        return "client/index";
    }

    /**
     * 异步刷新数据时，指定模板的数据容器id，进行页面的局部刷新
     * @param page 页码
     * @param size 页大小
     * @param model
     * @return
     */
    @RequestMapping("/queryArticleByPage")
    public String queryArticleByPage(Integer page, Integer size, Model model){
        Page<Article> articlePage = articleService.queryArticlePage(page, size);
        model.addAttribute("articlePage",articlePage);
        return "client/index::articles";
    }

}